package com.cm367.teamc;

import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

import com.cm367.teamc.bean.Leave;
import com.cm367.teamc.service.LeaveService;
import com.cm367.teamc.session.UserSession;

@ManagedBean
public class ConfirmLeave {

	@ManagedProperty(value = "#{leaveService}")
	private LeaveService leaveService;

	@ManagedProperty(value = "#{userSession}")
	private UserSession userSession;

	private String result = "";

	public LeaveService getLeaveService() {
		return leaveService;
	}

	public void setLeaveService(LeaveService leaveService) {
		this.leaveService = leaveService;
	}

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List<Leave> getUsersLeave() {
		List<Leave> list = leaveService.getUsersLeave(userSession
				.getDepartment());
		return list;
	}

	public String approve() {
		FacesContext fc = FacesContext.getCurrentInstance();
		Map<String, String> params = fc.getExternalContext()
				.getRequestParameterMap();
		String type = params.get("type");
		int leaveID = Integer.parseInt(params.get("LID"));
		if (type.equals("11")) {
			leaveService.changeStatus("Approved", leaveID);
		} else {
			leaveService.changeStatus("Approved Cancel Leave", leaveID);
		}
		result = "Leave ID " + leaveID + " has been approved.";
		return null;
	}

	public String reject() {
		FacesContext fc = FacesContext.getCurrentInstance();
		Map<String, String> params = fc.getExternalContext()
				.getRequestParameterMap();
		String type = params.get("type");
		int leaveID = Integer.parseInt(params.get("LID"));
		if (type.equals("11")) {
			leaveService.changeStatus("Rejected", leaveID);
		} else {
			leaveService.changeStatus("Rejected Cancel Leave", leaveID);
		}
		result = "Leave ID " + leaveID + " has been rejected.";
		return null;
	}

}
